package com.platform.api.bzproject.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.platform.api.bzproject.model.User;
import com.platform.api.bzproject.model.WalletDetails;
import com.platform.api.bzproject.model.WithdrawalDetails;
import com.platform.api.bzproject.dao.WithdrawalDetailsMapper;
import com.platform.api.bzproject.service.IWalletDetailsService;
import com.platform.api.bzproject.service.IWithdrawalDetailsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.api.bzproject.service.UserService;
import com.platform.common.annotation.RedisLock;
import com.platform.common.core.Constant;
import com.platform.common.core.Result;
import com.platform.common.exception.PlatformBindException;
import com.platform.common.util.GenerateSequenceUtil;
import com.platform.common.util.RedisUtil;
import com.platform.security.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 提现表 服务实现类
 * </p>
 *
 * @author guoshao
 * @since 2022-01-02
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WithdrawalDetailsServiceImpl extends ServiceImpl<WithdrawalDetailsMapper, WithdrawalDetails> implements IWithdrawalDetailsService {

    @Autowired
    private WithdrawalDetailsMapper withdrawalDetailsMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private IWalletDetailsService iWalletDetailsService;

    @Override
    public BigDecimal selectSumMoney(String userId) {
        return withdrawalDetailsMapper.selectSumMoney(userId);
    }

    @Override
    public PageInfo<WithdrawalDetails> selectWithdrawalDetails(WithdrawalDetails withdrawalDetails, Integer offset) {

        Integer pageNum = 1;
        if(offset > 0){
            pageNum = (offset / Constant.PAGESIZE) + 1;
        }
        PageHelper.startPage(pageNum, Constant.PAGESIZE);

        PageInfo<WithdrawalDetails> pageInfo = new PageInfo<>(withdrawalDetailsMapper.selectWithdrawalDetails(withdrawalDetails));
        return pageInfo;
    }

    @Override
    public boolean addWithdrawalDetails(WithdrawalDetails withdrawalDetails) {

        String userId = SecurityUtils.getUser().getUserId();

        String key = Constant.WITHDRAWAL_USER_ID + userId;

        if(null != RedisUtil.get(key)){
            throw new PlatformBindException("一分钟只能提现一次");
        }
        RedisUtil.set(key,userId,Constant.WITHDRAWAL_TIME);
        //扣除用户余额
        User user = userService.getUserByUserId(userId);

        if(withdrawalDetails.getMoney().compareTo(user.getBalance()) == 1){
            RedisUtil.del(key);
            throw new PlatformBindException("余额不足");
        }

        Date date = new Date();
        withdrawalDetails.setUserId(userId);
        withdrawalDetails.setApplyTime(date);
        withdrawalDetails.setStatus(1);

        withdrawalDetails.setFlowNo(Constant.ORDER_TX_PREFIX + GenerateSequenceUtil.generateSequenceNo());
        withdrawalDetailsMapper.insert(withdrawalDetails);
        user.setBalance(user.getBalance().subtract(withdrawalDetails.getMoney()));
        userService.updateById(user);

        //添加钱包明细-支出
        WalletDetails walletDetails = new WalletDetails();
        walletDetails.setUserId(userId);
        walletDetails.setMoney(withdrawalDetails.getMoney());
        walletDetails.setCreateTime(new Date());
        walletDetails.setPlatform(1);
        walletDetails.setDataType(2);
        walletDetails.setFlowNo(GenerateSequenceUtil.generateSequenceNo());
        iWalletDetailsService.save(walletDetails);

        return true;
    }
}
